//
//  FFTransferVM.m
//  LiMaoTransfer
//
//  Created by tt on 2020/9/17.
//

#import "FFTransferVM.h"
#import "FFTransferUserCell.h"
#import "FFTransferInputCell.h"
#import <LLWallet/LLWallet.h>
@interface FFTransferVM ()
@property(nonatomic,copy) NSString *amount;
@property(nonatomic,copy) NSString *remark;
@end

@implementation FFTransferVM

- (NSArray<NSDictionary *> *)tableSectionMaps {
   WKChannelInfo*channelInfo = [[WKSDK shared].channelManager getChannelInfo:[[WKChannel alloc] initWith:self.uid channelType:WK_PERSON]];
    __weak typeof(self) weakSelf = self;
    return @[
        @{
            @"height": @(0.01f),
            @"items": @[
                    @{
                        @"class": FFTransferUserModel.class,
                        @"avatar": [WKAvatarUtil getAvatar:self.uid],
                        @"name": channelInfo.displayName?:@"",
                    }
            ],
        },
        @{
            @"height": @(10.0f),
            @"items": @[
                    @{
                        @"class": FFTransferInputModel.class,
                        @"showBottomLine":@(YES),
                        @"onChange":^(NSString* value){
                            self.amount = value;
                        },
                        @"value": self.amount?:@"",
                        @"remark": weakSelf.remark?:@"",
                        @"onRemark":^(NSString *remark){ // 备注
                            weakSelf.remark = remark;
                            [weakSelf reloadData];
                        },
                        @"onDone":^{
                            if(!self.amount || self.amount.floatValue<=0) {
                                [[WKNavigationManager shared].topViewController.view showHUDWithHide:LLangW(@"转账金额不能等于或小于0！",weakSelf)];
                                return;
                            }
                            // 获取paytoken
                            __weak typeof(self) weakSelf = self;
                            [[FFWalletManager shared] ff_getPayToken:[WKMoneyUtil yuan2Penny:@(self.amount.floatValue)].integerValue item:LIMPayItemTransfer remark:LLangW(@"转账", weakSelf) callback:^(NSString * _Nonnull payToken) {
                                
                                [[WKNavigationManager shared].topViewController.view showHUD];
                                [weakSelf transfer:payToken].then(^{
                                    [[WKNavigationManager shared].topViewController.view hideHud];
                                    [[WKNavigationManager shared] popViewControllerAnimated:YES];
                                }).catch(^(NSError *error){
                                    [[WKNavigationManager shared].topViewController.view switchHUDError:error.domain];
                                });
                            }];
                        }
                    }
            ],
        },
    ];
}

-(AnyPromise*) transfer:(NSString*)payToken {
   return [[WKAPIClient sharedClient] POST:@"transfers" parameters:@{
       @"pay_token":payToken?:@"",
       @"amount": [WKMoneyUtil yuan2Penny:@(self.amount.floatValue)],
       @"to_uid": self.uid,
       @"remark": self.remark?:@"",
    }];
}


@end
